clear
set more off
version 14.0


local var_list ppt ET0 netppt0 water_deficit water_surplus deficitOff surplusOff gdd dday30C dday31C dday32C dday33C dday34C dday35C dday36C tmax tmin tavg
local var_list_star ppt* ET0* netppt0* water_deficit* water_surplus* deficitOff* surplusOff* gdd* dday30C* dday31C* dday32C* dday33C* dday34C* dday35C* dday36C* tmax* tmin* tavg* chg*
local model_list "ACCESS1-0 bcc BNU-ESM CanESM2 CCSM4 CESM1-BGC CNRM-CM5 CSIRO-Mk3-6-0 inmcm4 IPSL-CM5A-LR IPSL-CM5A-MR MIROC5 MIROC-ESM MIROC-ESM-CHEM MPI-ESM-LR MPI-ESM-MR MRI-CGCM3 NorESM1-M"
local scenario_list "historical _rcp45 _rcp85"
local scenario_short "his _rcp45 _rcp85"
local future_scenarios "rcp45 rcp85"

* Merge MoreDday and Offseason
foreach model of local model_list {
foreach s of local scenario_list {
	use "..\dataRaw\county_climate_`model'_`s'", clear
	merge 1:1 stcofips using "..\dataRaw\county_climateMoreDday_`model'_`s'", nogen
	merge 1:1 stcofips using "..\dataRaw\county_climate_OffSeason_`model'_`s'", nogen
	save "..\temp\county_climateAll_`model'_`s'", replace
} // end of model loop
} // end of scenario loop

* Append all models and scenarios into a single dataset
clear
foreach model of local model_list {
foreach s of local scenario_list {
	capture append using "..\temp\county_climateAll_`model'_`s'"
	capture gen model="`model'"
	capture gen scenario="`s'"
	replace model="`model'" if model==""
	replace scenario="`s'" if scenario==""
} // end of model loop
} // end of scenario loop

* Change historical scenario name so variable name is not so long
replace scenario="his" if scenario=="historical"
* Rename off season variables so not too long
ren water_deficitOffSeason deficitOff
ren water_surplusOffSeason surplusOff

gen gdd=(dday10C-dday30C)/100
keep stcofips model scenario `var_list' 

* convert from mm to inches
local varlist_mm ppt ET0 netppt0 water_deficit water_surplus deficitOff surplusOff
foreach var in `varlist_mm' {
	qui replace `var'=0.0393701*`var'
}

reshape wide `var_list', i(stcofips model) j(scenario) string

foreach fs of local future_scenarios {
foreach var of local var_list {
	gen chg_`var'_`fs'=`var'_`fs' - `var'his
}
}

*merge m:1 stcofips using "..\dataRaw\lrr_county_overlay", nogen keep(match master)
*keep if lrrsym=="M" | lrrsym=="H" | lrrsym=="F" | lrrsym=="G" | lrrsym=="O" | lrrsym=="L" 

summ water_deficithis water_surplushis ppthis ET0his dday32Chis gddhis

summ water_deficit*
summ water_surplus*
summ ppt*
summ ET0*
summ dday32C*
summ gdd*

summ chg_water_deficit*
summ chg_water_surplus*
summ chg_ppt*
summ chg_ET0*
summ chg_dday32C*
summ chg_gdd*

* change model names so they can be used as Stata variable names
replace model="ACCESS" if model=="ACCESS1-0"
replace model="BNUESM" if model=="BNU-ESM"
replace model="CESM1BGC" if model=="CESM1-BGC"
replace model="CNRMCM5" if model=="CNRM-CM5"
replace model="CSIROMk3" if model=="CSIRO-Mk3-6-0"
replace model="IPSLLR" if model=="IPSL-CM5A-LR"
replace model="IPSLMR" if model=="IPSL-CM5A-MR"
replace model="MIROCESM" if model=="MIROC-ESM"
replace model="MIROCCHEM" if model=="MIROC-ESM-CHEM"
replace model="MPIESMLR" if model=="MPI-ESM-LR"
replace model="MPIESMMR" if model=="MPI-ESM-MR"
replace model="MRICGCM3" if model=="MRI-CGCM3"
replace model="NorESM1M" if model=="NorESM1-M"


reshape wide `var_list_star', i(stcofips) j(model) string

local new_model_list "ACCESS bcc BNUESM CanESM2 CCSM4 CESM1BGC CNRMCM5 CSIROMk3 inmcm4 IPSLLR IPSLMR MIROC5 MIROCESM MIROCCHEM MPIESMLR MPIESMMR MRICGCM3 NorESM1M"


foreach fs of local scenario_short {
foreach var of local var_list {
	egen `var'`fs'AVG=rowmean(`var'`fs'*)
}
}

foreach fs of local future_scenarios {
foreach var of local var_list {
	gen chg_`var'_`fs'AVG=`var'_`fs'AVG - `var'hisAVG
}
}

*merge m:1 stcofips using "..\dataRaw\lrr_county_overlay", nogen keep(match master)
*keep if lrrsym=="M" | lrrsym=="H" | lrrsym=="F" | lrrsym=="G" | lrrsym=="O" | lrrsym=="L" 

summ water_deficithisAVG water_deficit_rcp45AVG water_deficit_rcp85AVG
summ water_surplushisAVG water_surplus_rcp45AVG water_surplus_rcp85AVG
summ ppthisAVG ppt_rcp45AVG ppt_rcp85AVG
summ ET0hisAVG ET0_rcp45AVG ET0_rcp85AVG
summ dday32ChisAVG dday32C_rcp45AVG dday32C_rcp85AVG
summ gddhisAVG gdd_rcp45AVG gdd_rcp85AVG

summ chg_water_deficit_rcp45AVG chg_water_deficit_rcp85AVG
summ chg_water_surplus_rcp45AVG chg_water_surplus_rcp85AVG
summ chg_ppt_rcp45AVG chg_ppt_rcp85AVG
summ chg_ET0_rcp45AVG chg_ET0_rcp85AVG
summ chg_dday32C_rcp45AVG chg_dday32C_rcp85AVG
summ chg_gdd_rcp45AVG chg_gdd_rcp85AVG
summ chg_deficitOff_rcp45AVG chg_deficitOff_rcp85AVG
summ chg_surplusOff_rcp45AVG chg_surplusOff_rcp85AVG

keep stcofips chg*
save "..\dataAnalysis\climate_scenarios", replace
